package com.yunupmall.controller.amdin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunupmall.api.fordVO.CommiditySpecesVo;
import com.yunupmall.api.fordVO.CommodityVo;
import com.yunupmall.api.fordentity.Commodity;
import com.yunupmall.api.fordentity.Specs;
import com.yunupmall.api.po.PagePO;
import com.yunupmall.entity.admin.commoditypojo.*;
import com.yunupmall.service.admin.service.CommodityService;
import com.yunupmall.uits.Result;
import com.yunupmall.uits.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/commodity")
public class CommodityController {
    @Autowired
    private  CommodityService commodityService;
    @Autowired
    private Result result;

    @RequestMapping("/selectcommodityDate")
    public String selectcommodityDate(@RequestParam("thispage") Integer thispage, @RequestParam("sizepage") Integer sizepage) {
        thispage = (thispage - 1) * sizepage;
        List<Commodity> commodity_spus = commodityService.selectDate(thispage, sizepage);
        String commodity = JSON.toJSONString(commodity_spus);
        KindCount kindCount = commodityService.getKindCount();
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", 0);
        result.put("msg", "");
        result.put("count", kindCount.getCounts());
        JSONArray array = JSONArray.parseArray(commodity);
        result.put("data", array);
        return JSON.toJSONString(result);
    }

    //商品数据分页
    @PostMapping("/page")
    public Result<Page<CommodityVo>> getcommoditywhereDate(@RequestBody CommodityVo commodityVo) {
       try {
          return result.ok(commodityService.pageData(commodityVo));
       }catch (Exception e) {
           return result.error(e.getMessage());
       }
    }

    /**
     * 商品删除
     */
    @GetMapping("/del")
    public Result<String> del(@RequestParam(value = "id") Integer id) {
        try {
            return result.ok(commodityService.del(id));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    //添加商品数据
    @PostMapping("/insert")
    public Result insertCommotity(@RequestBody CommodityVo commodityVo) {
        try {
            return result.ok(commodityService.insertData(commodityVo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    /**
     * 商品数据更新
     * @param commodityVo
     * @return
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody CommodityVo commodityVo) {
        try {
            return result.ok(commodityService.updateData(commodityVo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
    @RequestMapping("/addCommotityData")
    public void addCommotity(@RequestParam("BrandName") String BrandName, @RequestParam("KindName") String KindName) {
        System.out.println(BrandName + KindName);
    }


    @PostMapping("/getList")
    public Result<List<Commodity>> newcommotityID(@RequestBody CommodityVo commodityVo) {
        try {
            return result.ok(commodityService.newcommotityID(commodityVo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }


    //类别规格添加
    @PostMapping("/addskuDatas")
    public String addcommtityDatas(
            @RequestParam("kid") Integer kid,
            @RequestParam("dataresult") String dataresult) {
        List<ProductSpec> productSpecs = JSON.parseArray(dataresult, ProductSpec.class);
        List<ResultAdd> objects = new ArrayList<>();
        for (int i = 0; i < productSpecs.size(); i++) {
            String name = productSpecs.get(i).getName();
            String[] valueStr = productSpecs.get(i).getValue();
            for (int j = 0; j < valueStr.length; j++) {
                objects.add(new ResultAdd(name, valueStr[j]));
            }
        }
        int i = commodityService.insertcommotitySpecs(objects, kid);
        if (i > 0) {
            return "ok";
        }
        return "fail";
    }

    @PostMapping("/insertSpect_commotityData")
    public String insertSpect_commotityData(@RequestParam("datas") String datas, @RequestParam("c_id") Integer c_id) {
        List<Specs> specs = JSON.parseArray(datas, Specs.class);
        int i = commodityService.AddceatecommotitySpecs(specs, c_id);
        if (i > 0) {
            return "ok";
        }
        return "fail";
    }

    @GetMapping("/quetyKind_specsList")
    public List<SpecsName> quetyKind_specsList(Integer k_id) {
        List<SpecsName> specsNames = commodityService.quetyKind_specsList(k_id);
        return specsNames;
    }

    @GetMapping("/querySpecsUpData")
    public Map querySpecsUpData(String specsName, Integer k_id) {
        List<SpecsValues> specsValues = commodityService.querySpecsUpData(specsName, k_id);
        HashMap<String, Object> HashMap = new HashMap<>();
        HashMap.put("status", 1);
        HashMap.put("message", "ok");
        HashMap.put("result", specsValues);
        return HashMap;
    }


//    @GetMapping("/setThisID")
//    public void setThisID(Integer thisID) {
//        list.add(thisID);
//    }
//
//    @GetMapping("/querywhereSpercs")
//    public String querywhereSpercs() {
//        try {
//            List<SpecsCommodity> allData = commodityService.queryThisDataList(list.get(list.size() - 1));
//            String noReturnData = ReturnData.getNOReturnData(allData);
//            list.clear();
//            return noReturnData;
//        } catch (Exception e) {
//            System.out.println("数据越界");
//        }
//        return null;
//    }

//    @GetMapping("/deleteCommtityThis")
//
//    public String deleteCommtityThis(Integer id) {
//        System.out.println("结果" + id);
//        int i = commodityService.deleteCommtityThis(id);
//        if (i > 0) {
//            try {
//                int i1 = commodityService.deleteCommtityThissku(id);
//            } catch (Exception e) {
//                System.out.println("没有该SKU");
//            }
//            return "ok";
//        }
//        return "fail";
//    }
}
